<?php


namespace app\middleware;
// 引入类库
use think\wenhainan\Auth;

//thinkphp6.0权限扩展
class AuthOperate
{
    public function handle($request, \Closure $next)
    {
        // 获取auth实例
        $auth = Auth::instance();
        //获取前端header请求信息并把token信息转在字符串
        $token = (string)$request->header('token');
        //调用JWTAuth里面的checkAdminToken()方法对token解码
        $result = checkToken($token);
        $module = \think\facade\App::initialize()->http->getName();
        $controller = $request->controller();
        $action = $request->action();
        $request_url = $module.'/'.$controller .'/'. $action;
        //排除url
        $except_url = array('api/Role/getRoutes','admin/Index/getHandleAuthRole','api/Role/getRoutes2',
            'api/AuthRule/getMenus','api/AuthRule/getFatherData','admin/Index/headUpload');
        //如果当前不存在
        if(!in_array($request_url,$except_url)){
            //echo $request_url.'----------Uid'.$result['data']->uid;
           if(!$auth->check($request_url,$result['data']->uid)){
               return json(['status'=>502,'msg'=>'没有权限操作']);
           }
        }
        return $next($request);
    }
}